
import students_db                            # 导入学生成绩模块

class QueryResults:
    def __init__(self, file):
        self.user = {}

    def adduser(self, u):         # 添加用户
        self.user[u.user] = u     # u为用户实例
        return self

    def verify(self, u):       # 验证用户
        if u in self.user:
            for i in range(3):
                p = input('请输入密码：')
                if self.user[u].passwd == p:
                    return True
                else:
                    print('密码错误')
            else:
                print('{}用户已经被锁定,无法登陆'.format(u))
                return False
        else:
            print('没有此用户')
            return False

    def run(self):                            #  运行函数
        role = input('您登陆用户的角色是什么? 请输入： ')

        while True:                                   # 身份验证
            u = input('请输入用户名: ')
            if u == 'q':
                return None
            ret = self.verify(u)
            if ret == True:
                break

        if self.user[u].roles == 'teacher':              # 角色为老师时进入，添加成绩
            t1 = input('是否输入用户成绩: yes/no: ')
            if t1 == 'yes':
                while True:
                    data = input('请依次输入姓名、语文成绩、数学成绩、英语成绩：').split()
                    if data[0] == 'q':
                        t1 = 'no'
                        break
                    students_db.add_data(data)
        else:
            t1 = 'no'

        if t1 == 'no':                                       # 角色为学生或老师的进入，查询成绩
            t2 = input('是否查询学生成绩？yes/no ')
            if t2 == 'yes':
                while True:
                    name = input('请输入查询学生的姓名：')
                    if name == 'q':
                        break
                    print(students_db.query_data(name))
            elif t2 == 'no':
                return None

class Registered:                                             #   注册用户类
    def __init__(self, user, roles, passwd):
        self.user = user
        if roles == 'teacher' or roles == 'student':
            self.roles = roles
        else:
            raise Exception('roles error')
        self.passwd = passwd

# 测试
lin = Registered('lin','teacher', '123')
feng = Registered('feng', 'student', '123')
q = QueryResults('./score.txt')
q.adduser(lin).adduser(feng)             # 添加用户
q.run()                                  #  退出时，输入 q



